"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 兼容M*数据库date类型
Case Name   : date类型插入日期0值，不同输入格式验证
Create At   : 2023.08.09
Owner       : z@wan005
Description :
    1、创建数据库
    2、设置参数值，建表并插入0值日期验证,插入'YYYYMMDD'格式;插入YYYYMMDD格式;
    插入date加字符串格式;插入数值形式
    3、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class CompatibilityTest039(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.db_name = "db_date_0039"
        self.tb_name = "tb_date_0039"

    def test_atan(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        text = '-----step1:创建数据库 expect:成功-----'
        self.log.info(text)
        create_cmd = f"drop database if exists {self.db_name};" \
                     f"create database {self.db_name} dbcompatibility 'b';"
        msg = self.sh_primary.execut_db_sql(create_cmd)
        self.log.info(msg)
        self.assertIn('CREATE DATABASE', msg, '执行失败:' + text)

        text = '---step2:设置参数值，建表并插入0值日期验证 expect:成功--'
        self.log.info(text)
        sql_cmd = f"set dolphin.sql_mode='sql_mode_strict," \
                  f"sql_mode_full_group,pipes_as_concat,ansi_quotes';" \
                  f"create  table {self.tb_name} (c1 date);" \
                  f"insert {self.tb_name} values('00000000');" \
                  f"insert {self.tb_name} values(00000000);" \
                  f"insert {self.tb_name} values(date'00000000');" \
                  f"insert {self.tb_name} values(00000000::date);" \
                  f"insert {self.tb_name} values(cast(00000000 as date));" \
                  f"select * from {self.tb_name};"
        msg = self.sh_primary.execut_db_sql(sql_cmd,
                                            dbname=self.db_name)
        self.log.info(msg)
        self.assertIn('SET', msg, '执行失败:' + text)
        self.assertIn('CREATE TABLE', msg, '执行失败:' + text)
        self.assertEqual(5, msg.count('INSERT 0 1'), '执行失败:' + text)
        self.assertEqual(5, msg.count('0000-00-00'), '执行失败:' + text)

    def tearDown(self):
        text = '----step3:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.tb_name};"
        msg = self.sh_primary.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(msg)
        self.assertIn('DROP DATABASE', msg, '执行失败:' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
